package com.edeweiss.community.servlet;

import com.edeweiss.community.domain.Admin;
import com.edeweiss.community.domain.Hof;
import com.edeweiss.community.domain.HonorWall;
import com.edeweiss.community.services.AdminService;
import com.edeweiss.community.services.CommunityService;
import com.edeweiss.community.services.servicesImp.AdminServiceImp;
import com.edeweiss.community.services.servicesImp.CommunityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet {
    private AdminService adminService = new AdminServiceImp();
    //退出登陆
    public String exit(HttpServletRequest request,HttpServletResponse response) throws IOException {
       //销毁session
       request.getSession().invalidate();
       response.sendRedirect("loginForm.jsp");
       return "";
    }

    //管理员查询审批荣誉墙
    public String findHonorApprove(HttpServletRequest request,HttpServletResponse response) throws IOException {
        List<HonorWall> walls = adminService.findHonorApprove();
        if(walls.size()>0 && walls != null){
            //有未处理的申请
            request.getSession().setAttribute("walls",walls);
            response.sendRedirect("pages/honorApprove.jsp");
        }else {
            //没有要处理的申请
            request.getSession().setAttribute("msg","暂时没有要处理的申请");
            response.sendRedirect("pages/index.jsp");
        }
        return "";
    }


    //管理员审批荣誉墙
    public String honorApprove(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //处理结果
        String result = request.getParameter("result");
        //处理的社团
        String cid = request.getParameter("cid");

        int flag = adminService.honorApprove(Integer.parseInt(cid), Integer.parseInt(result));

        if(flag == 1){
            //有未处理的申请
            request.getSession().setAttribute("msg","处理成，请接着处理下一条请求！");
        }else {
            //没有要处理的申请
            request.getSession().setAttribute("msg","处理失败，请重新尝试！");
        }
        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //管理员查询审批解散社团
    public String findDissolveCommunity(HttpServletRequest request,HttpServletResponse response) throws IOException {
        List<HonorWall> dissolves = adminService.findDissolveCommunity();
        if(dissolves.size()>0 && dissolves != null){
            //有未处理的申请
            request.getSession().setAttribute("dissolves",dissolves);
            response.sendRedirect("pages/dissolveCommunity.jsp");
        }else {
            //没有要处理的申请
            request.getSession().setAttribute("msg","暂时没有要处理的申请");
            response.sendRedirect("pages/index.jsp");
        }
        return "";
    }

    //管理员解散社团
    public String dissolveCommunity(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //处理结果
        String result = request.getParameter("result");
        //处理的社团
        String cid = request.getParameter("cid");
        int flag = adminService.dissolveCommunity(Integer.parseInt(cid),Integer.parseInt(result));
        String msg = flag ==1 ?"处理成功，请接着处理下一个！":"处理失败，请重新尝试！";
        request.getSession().setAttribute("msg",msg);
        response.sendRedirect("pages/index.jsp");
        return "";
    }

    //查询需要申请名人堂的学生
    public String findHof(HttpServletRequest request,HttpServletResponse response) throws IOException {

        List<Hof> hofs = adminService.findHof();
        if(hofs != null){
            request.getSession().setAttribute("hofs",hofs);
            response.sendRedirect("pages/hofApprove.jsp");
        }else {
            request.getSession().setAttribute("msg","暂时没有申请需要处理！");
            response.sendRedirect("pages/index.jsp");
        }
        return "";
    }

    //处理需要加入名人堂的请求
    public String handlerHof(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String hid = request.getParameter("hid");
        String result = request.getParameter("result");
        String sid = request.getParameter("sid");

        int flag = adminService.handlerHof(Integer.parseInt(hid),Integer.parseInt(result),sid);

        String msg = flag ==1 ?"处理成功，请接着处理下一个！":"处理失败，请重新尝试！";
        request.getSession().setAttribute("msg",msg);
        response.sendRedirect("pages/index.jsp");
        return "";
    }
}
